#!/bin/bash

# 查看分布式存储系统状态脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== 分布式存储系统状态 ===${NC}"
echo ""

# 获取脚本所在目录
SCRIPT_DIR=$(cd $(dirname $0); pwd)
LOG_DIR="$SCRIPT_DIR/logs"

# 检查ChunkMaster状态
echo -e "${YELLOW}1. ChunkMaster状态:${NC}"
CHUNKMASTER_PIDS=$(pgrep -f "chunkmaster")
if [ ! -z "$CHUNKMASTER_PIDS" ]; then
    echo -e "${GREEN}✓ ChunkMaster运行中${NC}"
    for pid in $CHUNKMASTER_PIDS; do
        echo "  进程ID: $pid"
    done
    
    # 检查端口
    if lsof -Pi :50050 -sTCP:LISTEN -t >/dev/null 2>&1; then
        echo -e "${GREEN}✓ 端口 50050 监听中${NC}"
    else
        echo -e "${RED}✗ 端口 50050 未监听${NC}"
    fi
else
    echo -e "${RED}✗ ChunkMaster未运行${NC}"
fi
echo ""

# 检查ChunkServer状态
echo -e "${YELLOW}2. ChunkServer状态:${NC}"
CHUNKSERVER_PIDS=$(pgrep -f "chunkserver_server")
if [ ! -z "$CHUNKSERVER_PIDS" ]; then
    echo -e "${GREEN}✓ 找到 ${#CHUNKSERVER_PIDS[@]} 个ChunkServer进程${NC}"
    
    # 检查每个端口
    PORTS=(50051 50052 50053 50054 50055 50056)
    RUNNING_PORTS=()
    
    for port in "${PORTS[@]}"; do
        if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
            echo -e "${GREEN}  ✓ 端口 $port 监听中${NC}"
            RUNNING_PORTS+=($port)
        else
            echo -e "${RED}  ✗ 端口 $port 未监听${NC}"
        fi
    done
    
    echo ""
    echo -e "${BLUE}ChunkServer进程详情:${NC}"
    for pid in $CHUNKSERVER_PIDS; do
        echo "  进程ID: $pid"
        # 尝试获取进程的命令行参数
        CMD=$(ps -p $pid -o args= 2>/dev/null)
        if [ ! -z "$CMD" ]; then
            echo "  命令: $CMD"
        fi
    done
else
    echo -e "${RED}✗ 没有ChunkServer进程运行${NC}"
fi
echo ""

# 检查日志文件
echo -e "${YELLOW}3. 日志文件状态:${NC}"
if [ -d "$LOG_DIR" ]; then
    echo -e "${GREEN}✓ 日志目录存在: $LOG_DIR${NC}"
    
    # 检查ChunkMaster日志
    if [ -f "$LOG_DIR/chunkmaster.log" ]; then
        LOG_SIZE=$(ls -lh "$LOG_DIR/chunkmaster.log" | awk '{print $5}')
        echo "  ChunkMaster日志: $LOG_SIZE"
    else
        echo "  ChunkMaster日志: 不存在"
    fi
    
    # 检查ChunkServer日志
    PORTS=(50051 50052 50053 50054 50055 50056)
    for port in "${PORTS[@]}"; do
        LOG_FILE="$LOG_DIR/chunkserver_$port.log"
        if [ -f "$LOG_FILE" ]; then
            LOG_SIZE=$(ls -lh "$LOG_FILE" | awk '{print $5}')
            echo "  ChunkServer日志 (端口 $port): $LOG_SIZE"
        fi
    done
else
    echo -e "${RED}✗ 日志目录不存在${NC}"
fi
echo ""

# 检查PID文件
echo -e "${YELLOW}4. PID文件状态:${NC}"
if [ -f "$LOG_DIR/chunkmaster.pid" ]; then
    PID=$(cat "$LOG_DIR/chunkmaster.pid")
    if kill -0 $PID 2>/dev/null; then
        echo -e "${GREEN}✓ ChunkMaster PID文件有效: $PID${NC}"
    else
        echo -e "${RED}✗ ChunkMaster PID文件无效: $PID${NC}"
    fi
else
    echo "ChunkMaster PID文件: 不存在"
fi

PORTS=(50051 50052 50053 50054 50055 50056)
for port in "${PORTS[@]}"; do
    PID_FILE="$LOG_DIR/chunkserver_$port.pid"
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if kill -0 $PID 2>/dev/null; then
            echo -e "${GREEN}✓ ChunkServer PID文件 (端口 $port) 有效: $PID${NC}"
        else
            echo -e "${RED}✗ ChunkServer PID文件 (端口 $port) 无效: $PID${NC}"
        fi
    fi
done
echo ""

# 系统资源使用情况
echo -e "${YELLOW}5. 系统资源使用:${NC}"
if [ ! -z "$CHUNKMASTER_PIDS" ] || [ ! -z "$CHUNKSERVER_PIDS" ]; then
    echo "进程资源使用情况:"
    if [ ! -z "$CHUNKMASTER_PIDS" ]; then
        for pid in $CHUNKMASTER_PIDS; do
            CPU=$(ps -p $pid -o %cpu= 2>/dev/null)
            MEM=$(ps -p $pid -o %mem= 2>/dev/null)
            echo "  ChunkMaster (PID: $pid): CPU ${CPU}%, MEM ${MEM}%"
        done
    fi
    
    if [ ! -z "$CHUNKSERVER_PIDS" ]; then
        for pid in $CHUNKSERVER_PIDS; do
            CPU=$(ps -p $pid -o %cpu= 2>/dev/null)
            MEM=$(ps -p $pid -o %mem= 2>/dev/null)
            echo "  ChunkServer (PID: $pid): CPU ${CPU}%, MEM ${MEM}%"
        done
    fi
else
    echo "没有进程运行，无法获取资源使用情况"
fi
echo ""

# 总结
echo -e "${BLUE}=== 状态总结 ===${NC}"
if [ ! -z "$CHUNKMASTER_PIDS" ]; then
    echo -e "${GREEN}✓ ChunkMaster: 运行中${NC}"
else
    echo -e "${RED}✗ ChunkMaster: 未运行${NC}"
fi

if [ ! -z "$CHUNKSERVER_PIDS" ]; then
    echo -e "${GREEN}✓ ChunkServer: ${#CHUNKSERVER_PIDS[@]} 个进程运行中${NC}"
else
    echo -e "${RED}✗ ChunkServer: 未运行${NC}"
fi

echo ""
echo -e "${YELLOW}常用命令:${NC}"
echo "  启动ChunkMaster: ./start_chunkmaster.sh"
echo "  启动单个ChunkServer: ./start_chunkserver.sh [端口]"
echo "  启动多个ChunkServer: ./start_multiple_chunkservers.sh"
echo "  停止ChunkMaster: ./stop_chunkmaster.sh"
echo "  停止ChunkServer: ./stop_chunkserver.sh [端口]"
echo "  停止所有ChunkServer: ./stop_multiple_chunkservers.sh"
echo "  查看日志: tail -f logs/[进程名].log" 